package com.manager.web;

import com.manager.entity.Petshow;
import com.manager.service.PetService;
import com.manager.service.SpeciesService;
import com.manager.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetService petService;
    /*@Autowired
    private SpeciesMapper speciesMapper;*/
    /*【问题：Controller可以调用Mapper层吗？还是需要一层一层？】*/
    @Autowired
    private SpeciesService speciesService;
    @Autowired
    private StoreService storeService;
    /*根据id获取storeid*/

    @RequestMapping(value = "/petrelease",method = RequestMethod.GET)
    public String petrelease(@RequestParam("userid") String userid, Model model){
        model.addAttribute("userid",userid);
        model.addAttribute("specieslist",speciesService.getAll());
        /*这里把所有东西返回出来。【要检错吗？？？】*/
        model.addAttribute("storeid",storeService.getStoreidByUserid(userid));
        /*直接获取，我需要的值即可。*/
        return "petreleasepage";
    }

    /*前后端编码不一样怎么解决？produce有什么用？*/
    /*,produces = {"application/json; charset=utf-8"}这个去掉？为什么成功了还error【？？】*/
    @RequestMapping(value = "/releaseaction",method = RequestMethod.GET)
    @ResponseBody
    public Object releaseaction(@RequestParam("storeid") String storeid,
                                @RequestParam("petname") String petname,
                                @RequestParam("petphotopath") String petphotopath,
                                @RequestParam("gender") boolean gender,
                                @RequestParam("age") int age,
                                @RequestParam("health") String health,
                                @RequestParam("species") String species,
                                @RequestParam("money") int money){
        /*这么多数据，终于对接成功。下一步测试。插入三个表里面。写在Service层即可。这里简单调用。对接即可。不做逻辑处理？*/
        /*根据所有海量参数，调用Service业务层处理发布动作即可。返回一个String作为提示信息。*/
        String info=petService.petreleaseaction(storeid,petname,petphotopath,gender,age,health,species,money);
        // TODO: 2019/5/4 这里需要写啊？？？

        /*返回内层信息即可。又是不断返回的一个过程。这就是Java，对象的返回值？【参数亦如此。】的嵌套*/
        return info;
        /*还有个前后端代码不一致的问题？？？*/
    }

    /*参考。*/
    /*@RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE;charset=utf-8")*/

    @RequestMapping(value = "/deleteaction",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteaction(@RequestParam("petid") int petid){
        String string=petService.deteleaction(petid);
        return string;

        /*java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`github`.`pettradinglist`, CONSTRAINT `pettradinglist_petrelease_petid_fk` FOREIGN KEY (`petid`) REFERENCES `petrelease` (`petid`))*/
        /*这是什么错误？？之前不是没有的吗？？？【】*/
        /*delete from petrelease     where petid = ?【不是同时删除好几个的吗？？外键不是设置过了吗？级联删除？为什么删不了？】*/
        /*？*/
    }

    @RequestMapping(value = "/modifypage",method = RequestMethod.GET)
    public String modifypage(@RequestParam("petid") int petid,Model model){
        model.addAttribute("petid",petid);
        /*这里需要把所有这个的信息传过来？？或者查过来？？【】*/
        /*修改界面和插入界面差不多。*/
        Petshow petshow=petService.getPetInfoByPetid(petid);
        /*如果不是空指针异常，（不考虑，因为这个petid是通过前端数据传过来的。）*/
        model.addAttribute("petshow",petshow);
        /*传的一个对象？？？？【如何获取一个不是List的对象？？？【】】*/
        return "modifypage";
    }


    /**
     * 把所有他人的宠物拿出来。【？？】
     * @param userid
     * @param model
     * @return
     */
    @RequestMapping(value = "/allpetpage/{userid}",method = RequestMethod.GET)
    public String allpetpage(@PathVariable("userid") String userid,Model model){
        String storeid=storeService.getStoreidByUserid(userid);
        List<Petshow> allpetlist=petService.getAllPetInfoNotBuy(storeid);
        /*不是自己店里的。*/
        model.addAttribute("allpetlist",allpetlist);
        model.addAttribute("userid",userid);
        return "allpetpage";
    }

    @RequestMapping(value = "/buyaction",method = RequestMethod.GET)
    @ResponseBody
    public Object petbuyaction(@RequestParam("buyerid") String buyerid,@RequestParam("petid") int petid){

        /*买了之后。我们需要把flag设置为1即可？？？【表示已卖？？在购买记录添加购买记录“
        （买家+宠物id+时间+自己的id（自增。）？？）】
        【新建表？？】【ok】*/

        /*根据petid来改变该宠物的出售状况。*/
        String string=petService.petselling(buyerid,petid);
        /*【异常捕获。】*/
        return string;
    }

    @RequestMapping(value = "/modifyaction",method = RequestMethod.GET)
    @ResponseBody
    public Object modifyaction(@RequestParam("petid") int petid,
                               @RequestParam("petname") String petname,
                               @RequestParam("petphotopath") String petphotopath,
                               @RequestParam("health") String health,
                               @RequestParam("money") int money){

        return petService.petchangeInfo(petid,petname,petphotopath,health,money);
    }

    /*搜索功能添加。【？？？】*/

    @RequestMapping(value = "/searchaction",method = RequestMethod.GET)
    @ResponseBody
    public Object searchaction(@RequestParam(defaultValue = "") int petid,
                               @RequestParam(defaultValue = "") String petname,
                               @RequestParam(defaultValue = "") boolean gender,
                               @RequestParam(defaultValue = "") int age,
                               @RequestParam(defaultValue = "") String species,
                               @RequestParam(defaultValue = "") int money
                               ){
        /*需要的数据。*/
        /*BUG1无法将空传入，*/
        /*先检测，所有都输入。将后台写好。*/
        /*隔离这个问题。*/
        Map<String, Object> stringObjectMap= petService.searchpet(petid, petname, gender, age, species, money,1);
        /*页面的问题？？？？绑定到哪里？？？【】？？？*/
        return stringObjectMap;

    }
}
